package cn.itcast.contorller.company;

import cn.itcast.base.BaseContorller;
import cn.itcast.domain.Company;
import cn.itcast.domain.PageBean;
import cn.itcast.service.company.CompanyService;
import cn.itcast.utils.UtilFuns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.UUID;

@Controller
@RequestMapping(value = "/company")
public class CompanyController extends BaseContorller {
    @Autowired
    private CompanyService companyService;

    @RequestMapping(name = "查询所有企业",value = "/list.do")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5")int pageSize){
        PageBean pageBean = companyService.findAll(pageNum,pageSize);
        request.setAttribute("pb",pageBean);
        return "company/company-list";
    }
    @RequestMapping(value = "/toAdd.do",name = "去访问新建页面")
    public String toAdd(){
    return "company/company-add";
    }

    @RequestMapping(value = "/edit.do",name = "保存新的企业信息")
    public  String eidt(Company company){
        /*如果company id是空 他就是增加企业*/
        if (UtilFuns.isEmpty(company.getId())){
            company.setId(UUID.randomUUID().toString());
            companyService.save(company);
        }else {
        companyService.updateCompany(company);
        }
        return  "redirect:/company/list.do";
    }
    @RequestMapping(value = "/toUpdate.do",name = "去修改页面")
    public String toUpdate(String id){
        Company company=  companyService.toUpdate(id);
        request.setAttribute("company",company);
            return "company/company-update";
    }
    @RequestMapping(value = "/delete.do",name = "删除企业信息")
    public String deleteCompany(String id){
    companyService.deleteCompany(id);
    return "redirect:/company/list.do";
    }


}
